import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _Symbol$iterator;

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class;

/**
 * 针对 customize 提供的一些帮助方法
 */
import { Bind } from 'lodash-decorators';
import React from 'react';
import intl from "../intl";
import { resolveRequire } from "../utils";
import { mapCustomize } from "./index";
export var FeatureMapStore = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), (_class = (_Symbol$iterator = Symbol.iterator, /*#__PURE__*/function () {
  // eslint can't parse this syntax
  function FeatureMapStore(module, feature) {
    var _this = this;

    _classCallCheck(this, FeatureMapStore);

    this.module = void 0;
    this.feature = void 0;

    this[_Symbol$iterator] = function () {
      return mapCustomize[Symbol.iterator]({
        module: _this.module,
        feature: _this.feature
      });
    };

    this.feature = feature;
    this.module = module;
  }
  /**
   * 获取数据
   *  获取到 返回 数据
   *  否则 返回 undefined
   * @param {String} key - map 的 key
   * @param {any} [config] - 预留配置
   * @return {any|undefined}
   */


  _createClass(FeatureMapStore, [{
    key: "get",
    value: function get(key) {
      return mapCustomize.get({
        module: this.module,
        feature: this.feature,
        key: key
      });
    }
    /**
     * 设置 entry 数据
     * @param {String} key - map 的 key
     * @param {any} data - map key 对应的 value
     * @param {any} [config] - 预留配置
     */

  }, {
    key: "set",
    value: function set(key, data) {
      mapCustomize.set({
        module: this.module,
        feature: this.feature,
        key: key,
        data: data
      });
      return this;
    }
    /**
     * 清空对应的 数据
     * @param {any} [config] - 预留配置
     */

  }, {
    key: "clear",
    value: function clear() {
      return mapCustomize.clear({
        module: this.module,
        feature: this.feature
      });
    }
    /**
     * 删除对应 storeMap key 的 entry
     * 删除成功 返回 true
     * 未设置 返回 false
     * @param {String} key - map 的 key
     * @param {any} [config] - 预留配置
     */

  }, {
    key: "delete",
    value: function _delete(key) {
      return mapCustomize.delete({
        module: this.module,
        feature: this.feature,
        key: key
      });
    }
    /**
     * 返回 对应 数据的 entries
     * @param {any} [config] - 预留配置
     */

  }, {
    key: "entries",
    value: function entries() {
      return mapCustomize.entries({
        module: this.module,
        feature: this.feature
      });
    }
    /**
     * 遍历对应的 数据
     * @param {Function} fn - 遍历方法 (value, key, map) => void
     * @param {?any} thisArg - 遍历方法对应的 this
     * @param {any} [config] - 预留配置
     */

  }, {
    key: "forEach",
    value: function forEach(fn, thisArg) {
      return mapCustomize.forEach({
        module: this.module,
        feature: this.feature,
        fn: fn,
        thisArg: thisArg
      });
    }
    /**
     * 是否存在 数据
     * 存在 返回 true
     * 不存在/未设置 返回 false
     * @param {String} key - map 的 key
     * @param {any} [config] - 预留配置
     */

  }, {
    key: "has",
    value: function has(key) {
      return mapCustomize.has({
        module: this.module,
        feature: this.feature,
        key: key
      });
    }
    /**
     * 返回 对应 数据的 keys
     * @param {any} [config] - 预留配置
     */

  }, {
    key: "keys",
    value: function keys() {
      return mapCustomize.keys({
        module: this.module,
        feature: this.feature
      });
    }
    /**
     * 返回 对应 数据的 values
     * @param {any} [config] - 预留配置
     */

  }, {
    key: "values",
    value: function values() {
      return mapCustomize.values({
        module: this.module,
        feature: this.feature
      });
    }
    /**
     * 获取对应数据的 @@iterator
     * @param {any} [config] - 预留配置
     */

  }, {
    key: "size",
    value:
    /**
     * 获取对应数据的 数量
     * 未设置 返回 0
     * 已设置 返回 数据 数量
     * @param {any} [config] - 预留配置
     */
    function size() {
      return mapCustomize.size({
        module: this.module,
        feature: this.feature
      });
    }
  }]);

  return FeatureMapStore;
}()), (_applyDecoratedDescriptor(_class.prototype, "get", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "get"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "set", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "set"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "clear", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "clear"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "delete", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "delete"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "entries", [_dec5], Object.getOwnPropertyDescriptor(_class.prototype, "entries"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "forEach", [_dec6], Object.getOwnPropertyDescriptor(_class.prototype, "forEach"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "has", [_dec7], Object.getOwnPropertyDescriptor(_class.prototype, "has"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "keys", [_dec8], Object.getOwnPropertyDescriptor(_class.prototype, "keys"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "values", [_dec9], Object.getOwnPropertyDescriptor(_class.prototype, "values"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "size", [_dec10], Object.getOwnPropertyDescriptor(_class.prototype, "size"), _class.prototype)), _class));
/**
 * @param {string} cpaasModule
 * @param {string} feature
 */

export function mapCustomizeBuilder(cpaasModule, feature) {
  return new FeatureMapStore(cpaasModule, feature);
}
/**
 * LowCode 组件没有加载成功
 * @param componentCode
 * @returns {*}
 * @constructor
 */

export function DefaultNotFound(_ref) {
  var componentCode = _ref.componentCode;
  return React.createElement('div', undefined, intl.get('cpaas.common.error.message.sharedComponentError', {
    componentCode: componentCode
  }).d("shared <".concat(componentCode, " /> \u7EC4\u4EF6\u52A0\u8F7D\u5931\u8D25")));
}
/**
 * LowCode 组件加载中
 * @returns {null}
 * @constructor
 */

export function DefaultLoading(_ref2) {
  var componentCode = _ref2.componentCode;
  return React.createElement('div', undefined, intl.get('cpaas.common.error.message.sharedComponentLoading', {
    componentCode: componentCode
  }).d("shared <".concat(componentCode, " /> \u7EC4\u4EF6\u52A0\u8F7D\u4E2D")));
}

/**
 *
 * @param {string} module - 模块名
 * @param {string} [feature='component'] - 功能名
 * @param {object} [options] - 配置
 * @param {React.Component} [options.NotFount] - 没有加载到对应 componentCode 的组件时 渲染的组件
 * @param {React.Component} [options.Loading] - React.lazy/Suspense 加载组件时的fallback
 */
export function componentMapCustomizeBuilder(module) {
  var feature = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'component';
  var options = arguments.length > 2 ? arguments[2] : undefined;

  var _ref3 = options || {},
      _ref3$NotFound = _ref3.NotFound,
      NotFound = _ref3$NotFound === void 0 ? DefaultNotFound : _ref3$NotFound,
      _ref3$Loading = _ref3.Loading,
      Loading = _ref3$Loading === void 0 ? DefaultLoading : _ref3$Loading;

  var _mapCustomizeBuilder = mapCustomizeBuilder(module, feature),
      set = _mapCustomizeBuilder.set,
      get = _mapCustomizeBuilder.get;

  return {
    /**
     * @param {string} componentCode - 组件编码
     * @param factory - React.lazy 使用
     */
    setComponent: function setComponent(componentCode, factory) {
      return set(componentCode, factory);
    },

    /**
     *
     * @param {string} componentCode - 组件代码
     * @param {any} componentProps - 组件属性
     * @returns {React.SFCElement<React.SuspenseProps>}
     * @constructor
     */
    SharedComponent: function SharedComponent(_ref4) {
      var componentCode = _ref4.componentCode,
          componentProps = _ref4.componentProps;
      var CustomizeComponent = React.useMemo(function () {
        // TODO: 如果做微服务; get 方法应该本身就是异步的
        // 如果没有获取到组件编码 对应的数据/ 则应该认为失败
        return React.lazy(function () {
          return Promise.resolve((get(componentCode) || function () {
            return Promise.resolve({
              __esModule: true,
              default: function _default() {
                return React.createElement(NotFound, {
                  componentCode: componentCode
                });
              }
            });
          })()).then(function (cmp) {
            return {
              __esModule: true,
              default: resolveRequire(cmp)
            };
          });
        });
      }, []);
      return React.createElement(React.Suspense, {
        fallback: React.createElement(Loading, {
          componentCode: componentCode
        })
      }, React.createElement(CustomizeComponent, componentProps));
    }
  };
}